﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Revis.VClinicNet.Core.Domain
{
    public class Patient : IEquatable<Patient>
    {
        public virtual long Id { get; protected set; }
        public virtual PersonalData Personal { get; protected set; }
        public virtual Doctor Doctor { get; protected set; }
        public virtual Card Card { get; protected set; }
        public virtual IList<Visit> Visits { get; protected set; }

        #region Constructors

        public Patient()
        {
            SetUpCollections();
        }

        public Patient(PersonalData personal, Doctor doctor) :
            this(0, personal, doctor)
        {
        }

        public Patient(long id, PersonalData personal, Doctor doctor)
        {
            Id = id;
            SetUpCollections();
            SetUpFields(personal, doctor);
        }



        #endregion

        #region Public Methods

        public virtual void Change(PersonalData personal, Doctor doctor)
        {
            SetUpFields(personal, doctor);
        }

        public virtual void AddVisit(Visit visit)
        {
            if (visit == null) {
                throw new ArgumentNullException("Null visit");
            }
            if (visit.Patient.Id != Id) {
                throw new ArgumentException("Nie można dodać wizyty");
            }
            Visits.Add(visit);
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as Patient);
        }

        public virtual bool Equals(Patient other)
        {
            if (other == null) {
                return false;
            }
            return this.Id == other.Id;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode() ^ base.GetHashCode();
        }

        #endregion

        #region Private Methods

        private void SetUpCollections()
        {
            Visits = new List<Visit>();
            Card = new Card();
        }

        private void SetUpFields(PersonalData personal, Doctor doctor)
        {
            if (personal == null) {
                throw new ArgumentNullException("Null personalData");
            }
            if (doctor == null) {
                throw new ArgumentNullException("Null doctor");
            }
            Personal = personal;
            Doctor = doctor;
        }

        #endregion
    }
}
